package com.ruoyi.manager.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manager.domain.ArvinBookmarkCategory;
import com.ruoyi.manager.service.IArvinBookmarkCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 书签分类Controller
 * 
 * @author ruoyi
 * @date 2023-07-14
 */
@RestController
@RequestMapping("/bookmark/category")
public class ArvinBookmarkCategoryController extends BaseController
{
    @Autowired
    private IArvinBookmarkCategoryService arvinBookmarkCategoryService;

    /**
     * 查询书签分类列表
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArvinBookmarkCategory arvinBookmarkCategory)
    {
        // 设置当前登录用户ID，确保只能查询自己的数据
        arvinBookmarkCategory.setUserId(getUserId());
        
        startPage();
        List<ArvinBookmarkCategory> list = arvinBookmarkCategoryService.selectArvinBookmarkCategoryList(arvinBookmarkCategory);
        return getDataTable(list);
    }

    /**
     * 导出书签分类列表
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:category:export')")
    @Log(title = "书签分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ArvinBookmarkCategory arvinBookmarkCategory)
    {
        // 设置当前登录用户ID，确保只能导出自己的数据
        arvinBookmarkCategory.setUserId(getUserId());
        
        List<ArvinBookmarkCategory> list = arvinBookmarkCategoryService.selectArvinBookmarkCategoryList(arvinBookmarkCategory);
        ExcelUtil<ArvinBookmarkCategory> util = new ExcelUtil<ArvinBookmarkCategory>(ArvinBookmarkCategory.class);
        return util.exportExcel(list, "category");
    }

    /**
     * 获取书签分类详细信息
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        // 通过ID获取分类信息时需要验证是否属于当前用户
        ArvinBookmarkCategory category = arvinBookmarkCategoryService.selectArvinBookmarkCategoryById(id);
        if (category != null && !getUserId().equals(category.getUserId())) {
            return AjaxResult.error("无权访问此分类");
        }
        return AjaxResult.success(category);
    }

    /**
     * 新增书签分类
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:category:add')")
    @Log(title = "书签分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArvinBookmarkCategory arvinBookmarkCategory)
    {
        // 设置当前登录用户ID
        arvinBookmarkCategory.setUserId(getUserId());
        
        // 如果没有排序号，设置默认值
        if (arvinBookmarkCategory.getSort() == null) {
            arvinBookmarkCategory.setSort(1);
        }
        
        return toAjax(arvinBookmarkCategoryService.insertArvinBookmarkCategory(arvinBookmarkCategory));
    }

    /**
     * 修改书签分类
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:category:edit')")
    @Log(title = "书签分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArvinBookmarkCategory arvinBookmarkCategory)
    {
        // 修改前验证是否为当前用户的分类
        ArvinBookmarkCategory original = arvinBookmarkCategoryService.selectArvinBookmarkCategoryById(arvinBookmarkCategory.getId());
        if (original != null && !getUserId().equals(original.getUserId())) {
            return AjaxResult.error("无权修改此分类");
        }
        
        return toAjax(arvinBookmarkCategoryService.updateArvinBookmarkCategory(arvinBookmarkCategory));
    }

    /**
     * 删除书签分类
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:category:remove')")
    @Log(title = "书签分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        // 删除前验证是否为当前用户的分类
        for (Long id : ids) {
            ArvinBookmarkCategory category = arvinBookmarkCategoryService.selectArvinBookmarkCategoryById(id);
            if (category != null && !getUserId().equals(category.getUserId())) {
                return AjaxResult.error("无权删除他人的分类");
            }
        }
        
        return toAjax(arvinBookmarkCategoryService.deleteArvinBookmarkCategoryByIds(ids));
    }
} 